<?php
class InitComponent extends Object {
	var $controller = null;

	function startup(&$controller) {
		$this->controller = $controller;
	}

	/**
	 * Khởi tạo tất cả các controller
	 * @return unknown
	 */
	function init() {
		$this->controller->set('lang','vie');

		$login['User'] = $this->controller->Session->read('User.login');
		$login['Payment'] = $this->controller->Session->read('User.payment');

		$this->controller->set('basket', $this->controller->Session->read('User.order'));
		$this->controller->set('login', $login);

		return $login;
	}


	function getKeyword() {
		$this->controller->loadModel('Keyword');
		$_temp = $this->controller->Keyword->find('first');
		$this->controller->set('btx_description',$_temp['Keyword']['description']);
		$this->controller->set('btx_keyword',$_temp['Keyword']['keywords']);
		$this->controller->set('title_for_layout',$_temp['Keyword']['title']);
	}
  

	/**
	 * Khởi tao Danh mục sản phẩm ...
	 */
	function getProductType() {
		$this->controller->loadModel('ProductType');
		$this->controller->set(
			'productTypes',
			$this->controller->ProductType->getList()
		);
	}


	/**
	 * Get ProductType by store ...
	 * @param Member $store
	 */
	function getProductTypeStore($store) {
		$this->controller->loadModel('MemberProductType');
		$this->controller->set('productTypes',
			$this->controller->MemberProductType->getList($store)
		);
	}

	/**
	 * Gen Token
	 */
	function genToken() {
		$key = Security::generateAuthKey();
		$this->controller->set('_token', $key);
		$this->Session->write('_token', $key);
	}


	/**
	 * Lấy store hiện tại
	 */
	function getStore() {
		return $this->Session->read('User.store');
	}

	/**
	 * Login and return current page
	 */
	function loginReturn() {
		$this->controller->redirect(
			array(
				'controller'	=> 'login',
				'action'		=> 'index',
				'return'		=> base64_encode(
					$this->controller->params['url']['url']
				)
			)
		);
	}


	/*
	function hitCount($key = 'Music', $code) {
		$couter = Cache::read('hit_cache', 'couterCache');

		if(!empty($couter[$key][$code])) {
			$couter[$key][$code]++;
		} else {
			$couter[$key][$code] = 1;
		}

		$couter[$key]['total']++;

		if($couter['$key']['total'] * 100 == 0) {
			$this->loadModel('Music');
			foreach($couter[$key] as $k=>$v) {
				$this->Music->updateAll(
					array('hit' => "hit + $v"),
					array('id' => $k)
				);
			}
			$couter[$key] = null;
			$couter[$key]['total'] = 0;
		}

		Cache::write('hit_cache', $couter, 'couterCache');
	}
*/
}
?>
